package com.starbaba.system.index.dao;

import java.util.ArrayList;
import java.util.List;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.Strategy;
import com.starbaba.core.web.constant.WebConstants;
import com.starbaba.system.user.model.UserVO;
import com.tuanzi.server.loan_base.util.DateFormatFactory;

@SuppressWarnings("serial")
public class QueryUserOfRoleStrategy implements Strategy{
	
	private UserVO userVO;

	public QueryUserOfRoleStrategy(UserVO userVO) {
		this.userVO = userVO;
	}

	@Override
	@SuppressWarnings("unchecked")
	public Object process() throws Exception {
		String dtstr = DateFormatFactory.getDefaultDateFormat().format(new java.util.Date());
		StringBuffer sql = new StringBuffer();
		//查询用户角色表,获取用户的角色
		sql.append("select sur.roleid from sys_user_role sur ").
		append(" left join sys_role r on sur.roleid = r.roleid ").  
		append(" where sur.userid = ? and sur.starttime <=? and sur.stoptime >= ? ").  //用户权限有效时间控制
		append(" and r.status = ? and r.starttime <= ? and r.stoptime >= ? ");//角色状态、有效时间控制
		
		List<Long> roleIdList = DAOFactory.getInstance().createHibernateDAO().findBySQL(sql.toString(), null, new Object[]{userVO.getUserid(), dtstr, dtstr,WebConstants.STATUS_VALID,dtstr,dtstr});
		if(null == roleIdList) return new ArrayList<Long>();
		return roleIdList;
	}

	/**
	 * @return userVO
	 */
	
	public UserVO getUserVO() {
		return userVO;
	}

	/**
	 * @param userVO the userVO to set
	 */
	public void setUserVO(UserVO userVO) {
		this.userVO = userVO;
	}

}
